package com.skillworld.webapp.web.pages.rest.school;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.courseservice.CourseService;
import com.skillworld.webapp.model.courseservice.LessonInfo;
import com.skillworld.webapp.model.enrollment.Examination;
import com.skillworld.webapp.model.lesson.Lesson;
import com.skillworld.webapp.model.tutorial.MarkedTutorial.Mark;
import com.skillworld.webapp.model.tutorial.Tutorial;
import com.skillworld.webapp.model.tutorialservice.TutorialService;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.UserSession;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;

@ContentType("text/xml")
public class ViewLesson {
	
	@SuppressWarnings("unused")
    @Property
    private String errorMessage = null;
    
    @SuppressWarnings("unused")
    @Property
    private Lesson lesson;
    
    @SuppressWarnings("unused")
    @Property
    private boolean enrolled;
    
    @SuppressWarnings("unused")
    @Property
    private Examination examination;
    
    @SuppressWarnings("unused")
    @Property
    private Tutorial tut; //loop iterator
    
    @Inject
    private TutorialService tutorialService;
    
    @Inject
    private CourseService courseService;
    
    @SessionState(create=false)
    private UserSession userSession;
   
    private boolean userSessionExists;
    
    @Inject
    private Request request;
    
    
    /*
     * TODO FIX this, it is extremely inefficient (one query per tutorial)
     */
    public boolean isDone(Tutorial tutorial)
    { 
        return tutorialService
                    .findTutorialMarks(userSession.getUserId(), tutorial.getTutorialId())
                    .contains(Mark.DONE);
    }
    
    public boolean isLessonDone()
    {
        
        return false;
    }

    void onPassivate()
    {
        return;
    }
    
    void onActivate()
    {        
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        String lessonParam = request.getParameter("lesson");
        
        if (lessonParam == null)
        { 
            errorMessage = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
        
        try 
        {
            long lessonId = Long.parseLong(lessonParam);
            LessonInfo lessonInfo = courseService.viewLesson(lessonId,
                                                            userSession.getUserId());
        	lesson = lessonInfo.getLesson();
        	enrolled = lessonInfo.isEnrolled();
        	examination = lessonInfo.getExamination();
        }
        catch (NumberFormatException e)
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
        catch (InstanceNotFoundException e)
        { errorMessage = ErrorMessages.LESSON_NOT_FOUND; }  
    }
}
